package com.click.app.webapp.action;

import com.click.app.model.LabelValue;

import de.laures.cewolf.DatasetProduceException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.click.app.Constants;
import com.click.app.model.Employee;
import com.click.app.service.CutDetailManager;
import com.click.app.service.EmployeeManager;
import com.click.app.service.PantDetailManager;
import com.click.app.service.PaymentsCutManager;
import com.click.app.service.PaymentsPantManager;
import com.click.app.webapp.producers.ImageEmployeesReport;
import com.click.app.webapp.reportObjects.EmployeesReportRow;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author click
 */
public class EmployeesReportAction extends BaseAction {

    private static final long serialVersionUID = 295460450224891051L;
    private EmployeeManager employeeManager;
    private CutDetailManager cutDetailManager;
    private PantDetailManager pantDetailManager;
    private PaymentsCutManager paymentsCutManager;
    private PaymentsPantManager paymentsPantManager;
    private ImageEmployeesReport imageGenerate;
    private List<Employee> employees;
    private Date dateStart;
    private Date dateEnd;
    private String yearStart;
    private String yearEnd;
    private List<LabelValue> numYearList = new ArrayList<LabelValue>();
    private Map<String, String> typeReport;
    private Map<String, String> typeEmployee;
    private Map<String, String> typeComparation;
    private String typeReportChoice;
    private String typeEmployeeChoice;
    private String typeComparationChoice;

    private List<EmployeesReportRow> listEmployeesReport;
    private List listEmployeesReportHeaders;

    private Number totalReport = 0;

    public String execute() {

        typeEmployee = new HashMap<String, String >();
        getTypeEmployee().put("ROLE_EMPLOYEE_CUTTER", getText("typeEmployeeChoice.cutter"));
        getTypeEmployee().put("ROLE_EMPLOYEE_DRESSMAKER", getText("typeEmployeeChoice.dressmaker"));

        typeComparation = new HashMap<String, String >();
        getTypeComparation().put("1", getText("typeComparationChoice.perWork"));
        getTypeComparation().put("2", getText("typeComparationChoice.perPayments"));

        typeReport = new HashMap<String, String >();
        typeReport.put("1", getText("typeReportChoice.perMonth"));
        typeReport.put("2", getText("typeReportChoice.perYear"));
        
        SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
        Calendar currCalYearVar = Calendar.getInstance();
        Calendar currCalYear = Calendar.getInstance();
        currCalYear.add(Calendar.YEAR, -1);
        int j = 1;
        for (int i = currCalYear.get(Calendar.YEAR); i >= Constants.CONST_YEAR; i--, j++) {
            currCalYearVar.add(Calendar.YEAR, -1);
            String label = formatYear.format(currCalYearVar.getTime());
            numYearList.add(new LabelValue(label, j + ""));
        }

        return SUCCESS;
    }

    public List<LabelValue> getNumYearList() {
        return numYearList;
    }

    public void setNumYearList(List<LabelValue> numYearList) {
        this.numYearList = numYearList;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public String getYearEnd() {
        return yearEnd;
    }

    public void setYearEnd(String yearEnd) {
        this.yearEnd = yearEnd;
    }

    public String getYearStart() {
        return yearStart;
    }

    public void setYearStart(String yearStart) {
        this.yearStart = yearStart;
    }

    public String generateImageReport() throws DatasetProduceException {

            employees = employeeManager.findByRole(getTypeEmployeeChoice());

            boolean typePeriod = false;
            
            if (typeReportChoice.equals("2")) {
                dateStart = new Date("01/01/" + yearStart);
                dateEnd = new Date("12/31/" + yearEnd);
                typePeriod = true;
            }
            if(dateStart.compareTo(dateEnd) <= 0){
                imageGenerate = new ImageEmployeesReport(employeeManager, cutDetailManager, pantDetailManager, paymentsCutManager, paymentsPantManager, employees, typePeriod, typeComparationChoice, dateStart, dateEnd);
                imageGenerate.produceDataset(new HashMap());
                listEmployeesReport = new ArrayList(imageGenerate.getEmployeesReportRows().values());
                totalReport = 0;
                for(EmployeesReportRow rmr : listEmployeesReport){
                    totalReport = totalReport.floatValue() + rmr.getTotalValue().floatValue();
                }
                if(listEmployeesReport != null){
                    listEmployeesReportHeaders = listEmployeesReport.get(0).getCategories();
                }
            }else{
                 imageGenerate = null;
                 listEmployeesReport = null;
                 listEmployeesReportHeaders = null;
                 return ERROR;
            }
        return SUCCESS;
    }

    public String refreshImageGenerate() {
        return SUCCESS;
    }

    public ImageEmployeesReport getImageGenerate() {
        if (imageGenerate == null)
            imageGenerate = (ImageEmployeesReport) getSession().getAttribute("imageGenerate");
        
        return imageGenerate;
    }

    public void setImageGenerate(ImageEmployeesReport imageGenerate) {
        this.imageGenerate = imageGenerate;
    }

    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }

    public void setCutDetailManager(CutDetailManager cutDetailManager) {
        this.cutDetailManager = cutDetailManager;
    }

    public void setPantDetailManager(PantDetailManager pantDetailManager) {
        this.pantDetailManager = pantDetailManager;
    }

    public void setPaymentsCutManager(PaymentsCutManager paymentsCutManager) {
        this.paymentsCutManager = paymentsCutManager;
    }

    public void setPaymentsPantManager(PaymentsPantManager paymentsPantManager) {
        this.paymentsPantManager = paymentsPantManager;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public Map<String, String> getTypeReport() {
        return typeReport;
    }

    public Map<String, String> getTypeEmployee() {
        return typeEmployee;
    }

    public Map<String, String> getTypeComparation() {
        return typeComparation;
    }

    public String getTypeReportChoice() {
        return typeReportChoice;
    }

    public void setTypeReportChoice(String typeReportChoice) {
        this.typeReportChoice = typeReportChoice;
    }

    public String getTypeEmployeeChoice() {
        return typeEmployeeChoice;
    }

    public void setTypeEmployeeChoice(String typeEmployeeChoice) {
        this.typeEmployeeChoice = typeEmployeeChoice;
    }

    public String getTypeComparationChoice() {
        return typeComparationChoice;
    }

    public void setTypeComparationChoice(String typeComparationChoice) {
        this.typeComparationChoice = typeComparationChoice;
    }

    public List<EmployeesReportRow> getListEmployeesReport() {
        return listEmployeesReport;
    }

    public void setListEmployeesReport(List<EmployeesReportRow> listEmployeesReport) {
        this.listEmployeesReport = listEmployeesReport;
    }

    public List getListEmployeesReportHeaders() {
        return listEmployeesReportHeaders;
    }

    public void setListEmployeesReportHeaders(List listEmployeesReportHeaders) {
        this.listEmployeesReportHeaders = listEmployeesReportHeaders;
    }

    public Number getTotalReport() {
        return totalReport;
    }

    public void setTotalReport(Number totalReport) {
        this.totalReport = totalReport;
    }

    public String help(){
        return SUCCESS;
    }
}
